package com.android.semita;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	 TextView tvUserName ,tvPassword;
	 public static final String USERNAME="user_name";
	 public static final int LOGIN = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		tvUserName= (TextView)findViewById(R.id.etUsername);
		tvPassword= (TextView)findViewById(R.id.etPassword);	
	}
	 
	
	public void Login(View target) throws IOException {
		
		try{
			String userName = tvUserName.getText().toString();
			String password = tvPassword.getText().toString();
			
			new RefreshLogin(LoginActivity.this).execute(userName,password);
			
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void LoginProcess(boolean IsLoginSuccess, ProgressDialog dialog)
	{
		try
		{
			if(IsLoginSuccess)
			{
				String userName = tvUserName.getText().toString();
				Intent intent = new Intent(this, MainActivity.class);
				
				intent.putExtra(USERNAME, userName);		
				//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
								
				startActivity(intent);
				finish();
			}
			else
			{
				alert("Invalid username or password");
			}
		}
		finally
		{
			if (dialog.isShowing()) {
                dialog.dismiss();
            }
		}
		
		

	}
	
	private void alert(String message) {
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}
	
	private class RefreshLogin extends AsyncTask<String, Integer, String> {
			private String _userName , _password;
			private ProgressDialog dialog;
			private LoginActivity activity;
			private Context context;
			
			public RefreshLogin(LoginActivity activity) {
	            this.activity = activity;
	            context = activity;
	            dialog = new ProgressDialog(context);
	            dialog.setCancelable(true);
	        }
			
			@Override
			protected void onPreExecute() {
				this.dialog.setMessage("Waiting...");
	            this.dialog.show();
			}

			@Override
			protected String doInBackground(String... input) {
				
				_userName = input[0];
				_password = input[1];
				
				String RequestURL = "http://202.21.149.53/SemitaServices/api/login?username=" + _userName + "&password=" + _password;
				String returnString = "";
				
				try {
					JSONParser jsonParser = new JSONParser();						
					returnString = jsonParser.getJSONFromUrl(RequestURL);										
					
				} catch (Exception e) {
					e.printStackTrace();
				}			 

				return returnString;
			}
		
			@Override
			protected void onPostExecute(String LoginResultString) {
				//replace double quote 
				LoginResultString = LoginResultString.replace("\"", "");
							
				if(LoginResultString.equalsIgnoreCase("true"))
				{
					LoginProcess(true, dialog);
				}
				else
				{
					LoginProcess(false, dialog);
				}
			}
		}
	 
}
